package com.az.shiro.ice.main;

import com.az.shiro.ice.action.SystemOperation;
import com.az.shiro.ice.console.Console;
import com.az.shiro.ice.spring.ApplicationContextInstance;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionContext;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.subject.Subject;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;


/**
 * Main App Entrance
 */
public class App {

    //spring ioc容器示例
    private static final ClassPathXmlApplicationContext context = ApplicationContextInstance.getInstance();
    //用户主体
    private static Subject user;

    private static Long expireTime;

    public static void main(String[] args) {

        Console console = new Console();
        SystemOperation so = new SystemOperation();
        console.println(Console.LINE);


        while (!so.isLogined()) {
            console.println(Console.username_input_label);
            String username = console.readUsername();
            console.println(Console.password_input_label);
            String password = console.readPassword();
            user = so.login(username, password);
            console.println(Console.contact_label);
        }
        //打印登陆成功
        console.println(Console.login_success_label);
        //打印系统名称
        console.println(Console.TITLE);
        //打印控制台命令行
        console.println(Console.command_list_label);
        Session session =user.getSession();
        expireTime = session.getStartTimestamp().getTime() + session.getTimeout();

        //SimpleSession simpleSession = new SimpleSession()
        DefaultSessionManager manager = (DefaultSessionManager) context.getBean("sessionManager");
        //SimpleSession ss = manager.getSessionFactory().createSession();
        console.println("expire at : " + new Date(expireTime));
        while (user.isAuthenticated()) {
            try {
                console.println(new Date(session.getLastAccessTime().getTime()));
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        context.close();
    }
}
